﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMOVERTIME : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMOVERTIME()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select c.fdictname,b.username, a.*"
              +" from a_tc_overtime a,am_sys_user b, s_ts_dict c"
              +" where a.user_id = b.id"
              + " and a.fbillstate = c.fdictno and c.fdicttype = 2016";

            if (qiyear.Text != "")
            {
                SQL = SQL + " and year(fbegdate)=" + qiyear.Text;
            }

            DataRowView drv = (DataRowView)cbfuser.SelectedItem;
            if ((drv != null) && (cbfuser.SelectedIndex > 0))
            {
                string suserid = drv.Row["id"].ToString();
                SQL = SQL + " and b.id='" + suserid + "'";
            }

            SQL = SQL + "  order by fbegdate desc";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;

        }

        private void FMOVERTIME_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_stock";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";

            //下拉框
            DataTable myUser = myCache.doGetDict("AM_SYS_USER", "");
            cbuser.DataSource = myUser;
            cbuser.DisplayMember = "username";
            cbuser.ValueMember = "id";

            DataRow dr = myUser.NewRow();
            dr["username"] = "请选择";
            dr["id"] = "0";
            myUser.Rows.InsertAt(dr, 0);
            cbfuser.DataSource = myUser;
            cbfuser.DisplayMember = "username";
            cbfuser.ValueMember = "id";
            cbfuser.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifnote.Text = "";
            uifbegdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uifenddate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            DataTable myNo = StaticClass.myDB.query("exec fun_billnumber 2000");
            if ((myNo != null) && (myNo.Rows.Count > 0))
            {
                uifid.Text = myNo.Rows[0][0].ToString();
            }

            cbhour1.SelectedIndex = cbhour1.Items.IndexOf("09");
            cbminu1.SelectedIndex = cbminu1.Items.IndexOf("00");
            cbhour2.SelectedIndex = cbhour2.Items.IndexOf("18");
            cbminu2.SelectedIndex = cbminu2.Items.IndexOf("00");

            sType = "A";
            doLoadData();
            uifnote.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifid.Text = uiDGView["fid", rowindex].Value.ToString();
                uifnote.Text = uiDGView["fnote", rowindex].Value.ToString();
                string dt = uiDGView["fbegdate", rowindex].Value.ToString();
                DateTime mydt = Convert.ToDateTime(dt);
                uifbegdate.Text = mydt.ToString("yyyy-MM-dd");

                dt = uiDGView["fenddate", rowindex].Value.ToString();
                mydt = Convert.ToDateTime(dt);
                uifenddate.Text = mydt.ToString("yyyy-MM-dd");

                string sfbegtime = uiDGView["fbegtime", rowindex].Value.ToString();
                string sfendtime = uiDGView["fendtime", rowindex].Value.ToString();

                string shour1 = sfbegtime.Substring(0, 2);
                string sminu1 = sfbegtime.Substring(3, 2);
                string shour2 = sfendtime.Substring(0, 2);
                string sminu2 = sfendtime.Substring(3, 2);

                if (cbhour1.Items.IndexOf(shour1) > -1)
                {
                    cbhour1.SelectedIndex = cbhour1.Items.IndexOf(shour1);
                }

                if (cbminu1.Items.IndexOf(sminu1) > -1)
                {
                    cbminu1.SelectedIndex = cbminu1.Items.IndexOf(sminu1);
                }

                if (cbhour2.Items.IndexOf(shour2) > -1)
                {
                    cbhour2.SelectedIndex = cbhour2.Items.IndexOf(shour2);
                }

                if (cbminu2.Items.IndexOf(sminu2) > -1)
                {
                    cbminu2.SelectedIndex = cbminu2.Items.IndexOf(sminu2);
                }
            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            int sfbillstate = int.Parse(uiDGView["fbillstate", rowindex].Value.ToString());

            if (sfbillstate > 1)
            {
                UIMessageTip.ShowOk("该单据不能修改");
                return;
            }

            sType = "B";

            doLoadData();

            uifnote.ReadOnly = true;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            int sfbillstate = int.Parse(uiDGView["fbillstate", rowindex].Value.ToString());

            if (sfbillstate > 1)
            {
                UIMessageTip.ShowOk("该单据不能删除");
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fnote", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fid", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'a_tc_overtime','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(cbuser, "加班人员不能为空")
                && CheckEmpty(uifnote, "加班申请不能为空");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }

            string dt1 = uifbegdate.Text;
            string dt2 = uifenddate.Text;
            if (Convert.ToDateTime(dt1) > Convert.ToDateTime(dt2))
            {
                this.ShowInfoDialog("开始日期不能大于结束日期");
                return;
            }

            DataRowView drv2 = (DataRowView)cbuser.SelectedItem;
            string suser_id = drv2.Row["id"].ToString();

            string sbegtime = cbhour1.Text + ":" + cbminu1.Text;
            string sendtime = cbhour2.Text + ":" + cbminu2.Text;

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        a_tc_overtime myData = context.a_tc_overtime.Find(int.Parse(uifid.Text));
                        if (myData is null)
                        {
                            a_tc_overtime myData2 = new a_tc_overtime();
                            myData2.fnote = uifnote.Text;
                            myData2.fid = int.Parse(uifid.Text);
                            string dt = uifbegdate.Text;
                            myData2.fbegdate = Convert.ToDateTime(dt);
                            dt = uifenddate.Text;
                            myData2.fenddate = Convert.ToDateTime(dt);
                            myData2.fbillstate = 1;
                            myData2.fbegtime = sbegtime;
                            myData2.fendtime = sendtime;
                            myData2.fsite_no = sSite_No;
                            myData2.user_id = int.Parse(suser_id);
                            context.a_tc_overtime.Add(myData2);
                            context.SaveChanges();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    a_tc_overtime myovertime = context.a_tc_overtime.Find(int.Parse(uifid.Text));
                    myovertime.fnote = uifnote.Text;
                    string dt = uifbegdate.Text;
                    myovertime.fbegdate = Convert.ToDateTime(dt);
                    dt = uifenddate.Text;
                    myovertime.fenddate = Convert.ToDateTime(dt);
                    myovertime.fbegtime = sbegtime;
                    myovertime.fendtime = sendtime;
                    myovertime.user_id = int.Parse(suser_id);
                    int result = context.SaveChanges();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        //执行审核代码
        protected override void OnSys11()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            int sfbillstate = int.Parse(uiDGView["fbillstate", rowindex].Value.ToString());

            if (sfbillstate > 1)
            {
                UIMessageTip.ShowOk("该单据不能审核");
                return;
            }

            string sBillNo = uiDGView["fid", rowindex].Value.ToString();

            if (this.ShowAskDialog("确定审核该加班单?"))
            {
                string SQL = "exec proc_biz_bizcenter 'a_tc_overtime','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);
                this.ShowInfoDialog(myTable.Rows[0][0].ToString());

                OnSys04();
            }
        }

        private void cbfuser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
